import { createApp } from 'vue';
import App from './App.vue';
import Router from './router';
import Store from './store';
import '@/index.less';
import { useUser } from '@/store';

const rawData = toRaw(Store.state.value);
/**
 * 在刷新页面之前将Store存储的信息放入sessionStorage
 */

window.onbeforeunload = () => {
    for (let [item, value] of Object.entries(rawData)) {
        sessionStorage.setItem(`${item}Store`, JSON.stringify(value));
    }
    sessionStorage.setItem(`itemKeys`, JSON.stringify(Object.keys(rawData)));
};
createApp(App).use(Router).use(Store).mount('#app');
const keys = JSON.parse(sessionStorage.getItem('itemKeys') || '[]');
for (let item of keys) {
    const rawData = JSON.parse(sessionStorage.getItem(`${item}Store`) || '{}');
    if (item === "user") {
        useUser().$patch(state => {
            for (let [key, value] of Object.entries(rawData)) {
                (state as any)[key] = value;
            }
        });
    }
    sessionStorage.removeItem(`${item}Store`);
}
sessionStorage.removeItem('itemKeys');